﻿using BigMonitoringWarningApi.Tool.Extension;
using GYCTOAManagementSystemNewApi.Model.API;
using GYCTOAManagementSystemNewApi.Tool;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BigMonitoringWarningApi.Model.ApiResult
{
    ///	日期：2021/8/5 11:18:11 
    ///	描述: ApiResult            
    /// </summary>
    public class ApiResult
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public ApiStatusCodeEnum StatusCode { get; set; }

        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Status { get; set; }

        /// <summary>
        /// 返回信息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        public ApiResult()
        {
            this.StatusCode = ApiStatusCodeEnum.Code500;
            this.Status = false;
            this.Message = StatusCode.GetEnumDescription();
        }
        /// <summary>
        /// 返回True
        /// </summary>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static ApiResult True(string message = "") => Result(message: message, Status: null, StatusCode: ApiStatusCodeEnum.Code200);
        /// <summary>
        /// 返回TRUE
        /// </summary>
        /// <param name="data">数据</param>
        public static ApiResult<TData> True<TData>(TData data) => Result(data);
        /// <summary>
        /// 返回True
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <returns></returns>
        public static ApiResult<TData> True<TData>() => Result(default(TData));
        /// <summary>
        /// 返回TRUE
        /// </summary>
        /// <param name="status">状态</param>
        public static ApiResult True(bool status) => Result(status);
        /// <summary>
        /// 返回TRUE
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="message">返回消息</param>
        public static ApiResult True(bool status, string message) => Result(status, message);
        /// <summary>
        /// 返回TRUE
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="successmessage">成功返回消息</param>
        /// <param name="errormessage">失败返回消息</param>
        public static ApiResult True(bool status, string successmessage, string errormessage)
        {
            if (status)
                return Result(status, successmessage);
            return Result(status, errormessage);
        }
        /// <summary>
        /// 返回True
        /// </summary>
        /// <typeparam name="TData">数据</typeparam>
        /// <param name="data">数据</param>
        /// <param name="StatusCode">Code</param>
        /// <returns></returns>
        public static ApiResult<TData> True<TData>(TData data, ApiStatusCodeEnum StatusCode) => Result(data, null, true, StatusCode);
        /// <summary>
        /// 返回TRUE
        /// </summary>
        /// <param name="StatusCode"></param>
        /// <returns></returns>
        public static ApiResult True(ApiStatusCodeEnum StatusCode) => Result("", true, StatusCode);

        /// <summary>
        /// 返回TRUE
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="message">消息</param>
        public static ApiResult<TData> True<TData>(TData data, string message) => Result(data, message);

        /// <summary>
        /// 返回False
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ApiResult False(string message = "") => Result(message, false, ApiStatusCodeEnum.Code500);

        /// <summary>
        /// 返回False
        /// </summary>
        /// <param name="StatusCode"></param>
        /// <returns></returns>
        public static ApiResult False(ApiStatusCodeEnum StatusCode) => Result(null, false, StatusCode);

        /// <summary>
        /// 返回False
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ApiResult<TData> False<TData>(TData data) => Result(data, null, false, ApiStatusCodeEnum.Code500);

        /// <summary>
        /// 返回False
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <param name="data">数据实体</param>
        /// <param name="message">消息对象</param>
        /// <returns></returns>
        public static ApiResult<TData> False<TData>(TData data, string message) => Result(data, message, false, ApiStatusCodeEnum.Code500);
       
        
        
        /// <summary>
        /// 返回False
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <param name="message">消息对象</param>
        /// <returns></returns>
        public static ApiResult<TData> False<TData>(string message) => Result(default(TData), message, false, ApiStatusCodeEnum.Code500);
        /// <summary>
        /// 基类返回结果
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="Status">返回状态</param>
        /// <param name="StatusCode">状态码Code</param>
        /// <returns></returns>
        public static ApiResult Result(string message = null, bool? Status = null, ApiStatusCodeEnum StatusCode = ApiStatusCodeEnum.Code200) => new ApiResult()
        {
            StatusCode = StatusCode,
            Status = Status.IsEmpty() ? StatusCode.Equals(ApiStatusCodeEnum.Code200) : Status.ToBool(),
            Message = message.IsEmpty() ? StatusCode.GetEnumDescription() : message
        };

        /// <summary>
        /// 基类返回结果
        /// </summary>
        /// <param name="Status">返回状态</param>
        /// <returns></returns>
        public static ApiResult Result(bool Status)
        {
            return Result(null, Status, ApiStatusCodeEnum.Code200);
        }

        /// <summary>
        /// 基类返回结果
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <param name="data">数据</param>
        /// <param name="message">消息</param>
        /// <param name="Status">返回状态</param>
        /// <param name="StatusCode">状态码Code</param>
        /// <returns></returns>
        public static ApiResult<TData> Result<TData>(TData data, string message = null, bool? Status = null, ApiStatusCodeEnum StatusCode = ApiStatusCodeEnum.Code200)
        {
            return new ApiResult<TData>()
            {
                Data = data,
                StatusCode = StatusCode,
                Status = Status.IsEmpty() ? StatusCode.Equals(ApiStatusCodeEnum.Code200) : Status.ToBool(),
                Message = message.IsEmpty() ? StatusCode.GetEnumDescription() : message
            };
        }
    }

    /// <summary>
    /// 携带Data 的 API返回对象
    /// </summary>
    /// <typeparam name="TData"></typeparam>
    public class ApiResult<TData> : ApiResult
    {
        private TData _data = default;
        /// <summary>
        /// 构造方法
        /// </summary>
        public ApiResult()
            : base()
        {
            _data = default;
        }
        /// <summary>
        /// 操作结果业务数据
        /// </summary>
        public TData Data
        {
            get
            {
                if (typeof(TData).BaseType == typeof(IEnumerator) && _data == null)
                    _data = Activator.CreateInstance<TData>();

                return _data;
            }
            set { _data = value; }
        }
    }
}
